
/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "TerrainTextureDialog.h"

#include <QtCore/QUrl>
#include <QtCore/QFileInfo>
#include <QtGui/QGridLayout>
#include <QtGui/QPushButton>
#include <QtGui/QMouseEvent>
#include <QtGui/QMessageBox>

#include "GPKFile.h"
#include "GameEngine.h"

namespace PQBuilder
{
	TerrainTextureListWidget::TerrainTextureListWidget(QWidget* parent)
		: QListWidget(parent)
	{
		this->setViewMode(QListView::IconMode);
		this->setGridSize(QSize(GRID_SIZE_X, GRID_SIZE_Y));
		this->setFlow(QListView::LeftToRight);
		this->setIconSize(QSize(ICON_SIZE, ICON_SIZE));
		
		this->setAcceptDrops(true);

		loadIcons();
	}

	QString TerrainTextureListWidget::getSelectedTextureName()
	{
		QListWidgetItem* item=this->currentItem();
		if(item!=NULL){
			return item->whatsThis();
		}
		return "";

	}

	void TerrainTextureListWidget::dragEnterEvent(QDragEnterEvent *evt)
	{
		if (evt->mimeData()->hasFormat("text/uri-list")) {//accept local file system's file
			evt->accept();
		}
	}

	void TerrainTextureListWidget::dropEvent(QDropEvent *evt)
	{
		if (evt->mimeData()->hasUrls()){
			QList<QUrl> urls = evt->mimeData()->urls(); // returns list of QUrl
			int size=urls.size();
			for(int i=0;i<size;i++){
				QString fileName=urls[i].toLocalFile();
				QFileInfo fileInfo(fileName);
				if(fileInfo.isFile()){//only accept image file such as .png .tga
					if(SEngine.addTerrainTextureFromLocalFS(fileInfo.filePath().toStdString())){
						QImage image(fileName);
						QImage::Format format=image.format();
						
						if(format==QImage::Format_Invalid){
							return;
						}
						QPixmap pixmap=QPixmap::fromImage(image);
						
						QListWidgetItem *item=new QListWidgetItem(QIcon(pixmap),fileInfo.fileName());
						item->setWhatsThis(fileInfo.fileName());
						item->setToolTip(fileInfo.fileName());
						this->addItem(item);
					}else{
						QMessageBox box;
						box.setText(QObject::tr("Fail to add texture file!More info on http:://pq3d.com"));
						box.exec();
					}

				}else{
					QMessageBox box;
					box.setText(QObject::tr("Fail to add texture file,must be a texture file.More info on http:://pq3d.com"));
					box.exec();
				}
			}
		}
	}

	void TerrainTextureListWidget::dragLeaveEvent(QDragLeaveEvent *evt)
	{
	}

	void TerrainTextureListWidget::dragMoveEvent(QDragMoveEvent *evt)
	{
	}


	
	void TerrainTextureListWidget::loadIcons()
	{
		this->clear();
		
		int cnt=SEngine.getNumTerrainTexture();
		for(int i=0;i<cnt;i++){
			PQEngine::GPKFile* file=SEngine.getTerrainTextureFile(i);
			if(!file||!file->ok()){
				return;
			}

			file->open();
			int len=file->length();
			char* buf=new char[len];
			file->read(buf,len);
			file->close();


			QByteArray bytes(buf,len);
			QImage image;
			image.loadFromData(bytes);
			QPixmap pixmap=QPixmap::fromImage(image);
			QString name=QString::fromStdString(file->getFileName());
			QListWidgetItem *item=new QListWidgetItem(QIcon(pixmap),name);
			item->setWhatsThis(name);
			item->setToolTip(name);
			this->addItem(item);
			
			delete buf;
		}

		if(cnt>0){
			this->setCurrentItem(this->item(0));
		}
	}


	TerrainTextureDialog* TerrainTextureDialog::_dialog=NULL;

	TerrainTextureDialog::TerrainTextureDialog(QWidget* parent)
		: QDialog(parent),_isOk(false)
	{
		QGridLayout* grid=new QGridLayout(this);
		QPushButton* okButton=new QPushButton(QObject::tr("OK"),this);
		QPushButton* cancelButton=new QPushButton(QObject::tr("Cancel"),this);

		_textureListWidget=new TerrainTextureListWidget(this);
		
		
		grid->addWidget(_textureListWidget,0,0,1,3);
		grid->addWidget(okButton,1,1);
		grid->addWidget(cancelButton,1,2);

		grid->setRowMinimumHeight(0,300);
		grid->setRowMinimumHeight(1,32);
		grid->setColumnMinimumWidth(0,300);
		grid->setColumnMinimumWidth(1,64);
		grid->setColumnMinimumWidth(2,64);

		this->setLayout(grid);
		this->setWindowTitle(QObject::tr("Add Terrain Texture Layer"));
		this->setModal(true);

		connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
		connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
	}

	QString TerrainTextureDialog::getTexturePath()
	{
		if(_dialog==NULL){
			_dialog=new TerrainTextureDialog();
		}
		_dialog->show();
		_dialog->exec();

		if(_dialog->isOK()){
			return _dialog->getSelectedTextureName();
		}else{
			return QString::null;
		}
	}

	void TerrainTextureDialog::okClicked()
	{
		_isOk=true;
		this->hide();
		
	}

	void TerrainTextureDialog::cancelClicked()
	{
		_isOk=false;
		this->hide();
	}
}
